define(function (require, exports, module) {

    var mCard = require('andrew/card/index.css');
    var _ = require("third/lodash.js");
    var toastr = require("third/toastr/index.js");

    var api = require('service/api.js');


    var UrlHelper = require('tools/urlHelper.js');

    var urlMap = UrlHelper.getUrlParamsMap();

    if (urlMap['id'] == null) {
        toastr.error("文章id不存在!");
        window.location.href = "04_artList.html";
        return false;
    }

    var artId = parseInt(urlMap['id']);

    api.getArtPartList({
        data: {
            "artId": artId,
            'dataFlag': 1
        },
        success: function (res) {

            for (let index = 0; index < res.length; index++) {
                var item = res[index];
                add(item);
            }

        }
    })

    $(".submit").click(function () {

        var arr = [];

        $("[area-id]").each(function () {

            var id = $(this).attr("area-id") || "";

            var item = {
                id: id,
                artId: artId
            };

            item.content = $('[name=content_' + id + ']').val();
            item.imgUrlOne = $('#imgUrlOne_' + id).attr('src') || "";
            item.imgUrlTwo = $('#imgUrlTwo_' + id).attr('src') || "";
            item.imgUrlThree = $('#imgUrlThree_' + id).attr('src') || "";

            item.imgType = $('#imgType_' + id).val() || "";
            item.imgHeight = $('#imgHeight_' + id).val() || "";
            arr.push(item);

        });

        for (let index = 0; index < arr.length; index++) {
            const element = arr[index];
            element.orderNum = index;
            if (element.id.indexOf('new_') > -1) {
                element.id = 0;
            }
            element.dataFlag = element.dataFlag || 1;
        }

        console.log(arr, 'arr');

        // return false;
        api.saveArtPart({
            data: arr,
            success: function () {
                toastr.success("操作成功!");
            }
        })



    });


    function add(item) {


        $("#one_tpl").tmpl(item).appendTo('.content-placeholder');


        KindEditor.create($("[area-id='" + item.id + "']"), {
            allowFileManager: true,
            afterChange: function () {
                this.sync();
            }
        });
    }

    $(".add").click(function () {

        var guid = "new_" + new Date().getTime();
        var new_item = {
            "id": guid,
            "content": "",
            "artId": 0,
            "imgType": 2,
            "imgUrlOne": "",
            "imgUrlTwo": "",
            "imgUrlThree": "",
            "dataFlag": 1,
            "imgHeight": 300,
            "orderNum": 10
        }

        add(new_item);

    });

    $("#app").on('click', '.del', function (e) {

        target = e.target || e.srcElement;

        var toTargetId = $(target).attr('target-id');


        if (toTargetId.indexOf('new_') > -1) {
            $("#card_" + toTargetId).slideUp().remove();
            toastr.success("移除成功!");
            return false;
        }

        layer.confirm('确定移除？', {
            btn: ['确定'] //按钮
        }, function (val, index) {
            var item = {
                "id": toTargetId,
                "dataFlag": 0
            }

            api.updateArtPart({
                data: item,
                success: function () {
                    toastr.success("操作成功");
                    layer.close(index);
                }
            })
        });



    });



    $("#app").on('click', '.select_img', function (e) {

        target = e.target || e.srcElement;

        var toTargetId = $(target).attr('target-id');

        layer.prompt({ title: '输入图片路径，并确认', formType: 2, width: 200 }, function (val, index) {

            $("#" + toTargetId).attr('src', val);
            layer.close(index);

        });

    });

    $("#app").on('click', '.clear_img', function (e) {

        target = e.target || e.srcElement;

        var toTargetId = $(target).attr('target-id');

        $("#" + toTargetId).attr('src', '');

    });


});